[CFArray release]: message sent to deallocated instance

Posted by arielcamus on Stack Overflow See other posts from Stack Overflow or by arielcamus
Published on 2010-03-19T16:45:16Z Indexed on 2010/03/19 17:11 UTC
Read the original article Hit count: 377

Hi,

I'm using the following method in my code:

- (NSMutableArray *) newOrderedArray:(NSMutableArray *)array ByKey:(NSString *)key ascending:(BOOL)ascending {

    NSSortDescriptor *idDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:ascending];

    NSArray *sortDescriptors = [NSArray arrayWithObject:idDescriptor];
    NSArray *orderArray = [array sortedArrayUsingDescriptors:sortDescriptors];

    [idDescriptor release];

    NSMutableArray *result = [NSMutableArray arrayWithArray:orderArray];

    return result;
}

Is this a well-coded convenience method? As I think, it returns an autoreleased NSMutableArray.

This method is called by another one:

- (id) otherMethod {

    NSMutableArray *otherResult = [[[NSMutableArray alloc] initWithCapacity:[otherArray count]] autorelease];

   // I add some stuff to otherResult and then...

    NSMutableArray *result = [dbUtils newOrderedArray:otherResult ByKey:@"objectId" ascending:NO];
    return result;
}

This method (otherMethod) is called in some view controller where I want to store returned array and release it when deallocating the view controller. However, when [result retain] is called in this view controller (because I need it to be available and I can't allow it to be deallocated) I receive the following error:

[CFArray release]: message sent to deallocated instance

I've tried to log [result retainCount] just before calling retain and it print "1". I don't understand why an error is thrown when calling retain.

Thank you,

A

© Stack Overflow or respective owner

Related posts about memory-management

Related posts about dealloc